package org.kugua.effect.impl;

import java.awt.*;
import java.awt.geom.AffineTransform;

import lombok.extern.slf4j.Slf4j;
import org.kugua.context.CaptchaContext;
import org.kugua.effect.IEffect;
import org.kugua.enums.QuestionAnswerType;
import org.kugua.utils.RandomUtils;

/**
 * 干扰线-效果
 *
 * @author 苦瓜
 * @create 2024-08-27
 * @since 1.0.0
 */
@Slf4j
public class AggressorLineEffect implements IEffect {

    /**
     * 添加干扰线-效果
     *
     * @param context 上下文信息
     */
    @Override
    public void effect(CaptchaContext context) {
        log.trace("AggressorLineEffect Effect Process.");
        if (QuestionAnswerType.IMAGE_ANSWER.getType().equals(context.getQuestionAnswerType())) {
            return;
        }
        Graphics2D graphics = context.getGraphics();
        for (int i = 0; i < 20; i++) {
            int x1 = RandomUtils.random(context.getWidth());
            int y1 = RandomUtils.random(context.getHeight());
            AffineTransform affineTransform = new AffineTransform();
            affineTransform.rotate(Math.toRadians(RandomUtils.random(90)), x1 + 10, y1 + 10);
            graphics.setTransform(affineTransform);
            graphics.setColor(new Color(RandomUtils.random(255),
                    RandomUtils.random(255),
                    RandomUtils.random(255)));
            // 设置线条粗细
            graphics.setStroke(new BasicStroke(2f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
            graphics.drawLine(x1, y1, x1 + 20, y1 + 20);
        }
        graphics.setTransform(new AffineTransform());
    }
}
